package com.seveneight.baijiesteward.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.JsonFormat;
import com.seveneight.baijiesteward.utils.MDialog;
import com.seveneight.baijiesteward.utils.MMessageDialog;
import com.seveneight.baijiesteward.utils.PreferencesUtil;
import com.seveneight.baijiesteward.utils.Setting;
import com.seveneight.baijiesteward.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by aaa on 2018/1/21.
 */

public abstract class BaseFragmentBack extends BaseBackFragment {

    public Activity mContext;
    public View view;
    public Setting setting;
    public MMessageDialog dialog;
    private MDialog mDialog;
    public JsonFormat jsonFormat;
    public UserBean userBean;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getContentView(), container, false);
        return attachToSwipeBack(view);
    }

    public <T extends View> T findView(int id) {
        return view.findViewById(id);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!StringUtils.isEmpty(PreferencesUtil.get(getContext(),PreferencesUtil.userinfo,"")))
            userBean= JSON.parseObject(PreferencesUtil.get(getContext(),PreferencesUtil.userinfo,""),UserBean.class);
        initView();
        initUser();
        initData();
        setListener();
    }
    //处理回调
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        if (messageEvent.getType()==110){
            userBean= JSON.parseObject(PreferencesUtil.get(getContext(),PreferencesUtil.userinfo,""),UserBean.class);
        }else if (messageEvent.getType()==111){
            userBean=null;
        }
        CallBus(messageEvent);
    }
    public void CallBus(MessageEvent messageEvent){

    }

    public void initUser() {
        setting = Setting.getInstance(mContext);
        jsonFormat = JsonFormat.getInstance(mContext);

    }

    protected abstract int getContentView();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void setListener();

    public void showDialog(String title){
        dialog = new MMessageDialog(mContext,title);
        dialog.showDialog();
    }


    public void showLoading(String title){
        mDialog = new MDialog(mContext,title);
        mDialog.showLoadingDialog();
    }

    public void hideLoading(){
        mDialog.hideDialog();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
